<?php
/*
# $Id$
*/

class BuanSourceFolder {

	private $template = '';
	private $source = '';
	private $target = '';
	private $parent = NULL;
	private $ignored = array('.svn');

	public function __construct($source, &$parent=NULL) {
		$this->source = $source;
		$this->parent = $parent;
	}

	public function setTarget($target) {
		$this->target = $target;
	}

	public function setTemplate($template) {
		$this->template = $template;
	}

	public function createTarget() {
		if(!file_exists(dirname($this->target))) {
			if($this->parent===NULL) {
				die("Cannot create parent target folder");
			}
			else {
				$this->parent->createTarget();
			}
		}
		if(!file_exists($this->target)) {
			mkdir($this->target);
		}
	}

	public function parse() {

		$children = new DirectoryIterator($this->source);
		foreach($children as $child) {
		
			// Parse files in sub-folder
			if($child->isDir() && !$child->isDot() && !in_array($child->getFilename(), $this->ignored)) {
				$dir = new BuanSourceFolder("{$this->source}/{$child->getFilename()}", $this);
				$dir->setTarget("{$this->target}/{$child->getFilename()}");
				$dir->setTemplate($this->template);
				$dir->parse();
			}

			// Parse child file
			else if($child->isFile()) {
				$file = new BuanSourceFile("{$this->source}/{$child->getFilename()}");
				$file->setTarget("{$this->target}/{$child->getFilename()}");
				$file->setTemplate($this->template);
				if($file->parse($document)) {
					$this->createTarget();
					file_put_contents("{$this->target}/".preg_replace("/\.[^\.]+$/i", ".html", $child->getFilename()), $document);
				}
				$document = '';
			}
		}

		// Create index file for this folder
	}
}
?>